package com.jzo2o.customer.controller.certification;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;

@RestController("CertificationController")
@RequestMapping
@Api(tags = "服务端/机构端-提交认证接口设计")
public class CertificationController {

    @Autowired
    private IWorkerCertificationAuditService workflowCertificationService;

    @Autowired
    private IWorkerCertificationService workerCertificationService;

    @Autowired
    private IAgencyCertificationService agencyCertificationService;

    @Autowired
    private IAgencyCertificationAuditService agencyAuditService;
    /**
     * 审核服务人员认证分页查询
     * @param
     * @return
     */
    @GetMapping("/operation/worker-certification-audit/page")
    @ApiOperation("审核服务人员认证分页查询")
    public PageResult<WorkerCertificationAudit> selectPage(WorkerCertificationAuditPageQueryReqDTO workflowCertificationAudit) {
        return workflowCertificationService.pageQury(workflowCertificationAudit);
    }

    /**
     * 审核服务人员认证消息
     * @return
     */
    @PutMapping("/operation/worker-certification-audit/audit/{id}")
    @ApiOperation("审核服务人员认证消息")
    public void auditWorker(@PathVariable("id") Long id, Integer certificationStatus, String rejectReason ) {
        workflowCertificationService.update(Wrappers.<WorkerCertificationAudit>lambdaUpdate().eq(WorkerCertificationAudit::getId,id).set(WorkerCertificationAudit::getAuditStatus,1).set(WorkerCertificationAudit::getCertificationStatus,certificationStatus).set(WorkerCertificationAudit::getRejectReason,rejectReason).set(WorkerCertificationAudit::getAuditorId,UserContext.currentUserId()).set(WorkerCertificationAudit::getAuditorName,"暂空").set(WorkerCertificationAudit::getAuditTime,new Date()));
        workerCertificationService.update(Wrappers.<WorkerCertification>lambdaUpdate().eq(WorkerCertification::getId,id).set(WorkerCertification::getCertificationStatus,certificationStatus).set(WorkerCertification::getCertificationTime,new Date()));
    }

    /**
     * 服务端-提交认证申请
     * @param workflowCertificationAudit
     * @return
     */
    @PostMapping("/worker/worker-certification-audit")
    @ApiOperation("服务端-提交认证申请")
    public void addWorkerCertification(@RequestBody WorkerCertificationAuditAddReqDTO workflowCertificationAudit) {
        if (ObjectUtil.isNotEmpty(workflowCertificationAudit)){
            WorkerCertificationAudit workerCertification =  new WorkerCertificationAudit();
            WorkerCertification certification =  new WorkerCertification();
            BeanUtil.copyProperties(workflowCertificationAudit,workerCertification);
            BeanUtil.copyProperties(workflowCertificationAudit,certification);
            WorkerCertificationAudit byId = workflowCertificationService.getById(UserContext.currentUserId());
            if (ObjectUtil.isNotEmpty(byId)){
                workerCertification.setAuditStatus(0);
                workerCertification.setCertificationStatus(1);
                workerCertification.setRejectReason("");
                workflowCertificationService.update(workerCertification,null);
                workerCertificationService.update(Wrappers.<WorkerCertification>lambdaUpdate().eq(WorkerCertification::getId,byId.getId()).set(WorkerCertification::getCertificationStatus,1));
            }else {
                workerCertification.setId(UserContext.currentUserId());
                certification.setId(UserContext.currentUserId());
                workflowCertificationService.save(workerCertification);
                certification.setCertificationStatus(1);
                workerCertificationService.save(certification);
            }
        }else {
            throw new BadRequestException("认证信息不能为空！！");
        }
    }

    /**
     * 服务端-查询最新的驳回原因
     *
     * @return
     */
    @GetMapping("/worker/worker-certification-audit/rejectReason")
    @ApiOperation("服务端-查询最新的驳回原因")
    public HashMap<String, String> selecrWorker() {
        HashMap<String, String> hashMap = new HashMap<>();
        String rejectReason = workflowCertificationService.getById(UserContext.currentUserId()).getRejectReason();
        hashMap.put("rejectReason",rejectReason);
        return hashMap;
    }

    /**
     * 审核机构认证分页查询
     * @param
     * @return
     */
    @GetMapping("/operation/agency-certification-audit/page")
    @ApiOperation("审核机构认证分页查询")
    public PageResult<AgencyCertificationAudit> selectAgencyPage(AgencyCertificationAuditPageQueryReqDTO agencyCertificationAudit) {
        return agencyAuditService.pageQury(agencyCertificationAudit);
    }

    /**
     * 审核机构人员认证消息
     * @return
     */
    @PutMapping("/operation/agency-certification-audit/audit/{id}")
    @ApiOperation("审核机构人员认证消息")
    public void auditAgency(@PathVariable("id") Long id, Integer certificationStatus, String rejectReason ) {
        agencyAuditService.update(Wrappers.<AgencyCertificationAudit>lambdaUpdate().eq(AgencyCertificationAudit::getId,id).set(AgencyCertificationAudit::getAuditStatus,1).set(AgencyCertificationAudit::getCertificationStatus,certificationStatus).set(AgencyCertificationAudit::getRejectReason,rejectReason).set(AgencyCertificationAudit::getAuditorId,UserContext.currentUserId()).set(AgencyCertificationAudit::getAuditorName,"暂空").set(AgencyCertificationAudit::getAuditTime,new Date()));
        agencyCertificationService.update(Wrappers.<AgencyCertification>lambdaUpdate().eq(AgencyCertification::getId,id).set(AgencyCertification::getCertificationStatus,certificationStatus).set(AgencyCertification::getCertificationTime,new Date()));
    }

    /**
     * 机构端-提交认证申请
     * @param agencyCertificationAudit
     * @return
     */
    @PostMapping("/agency/agency-certification-audit")
    @ApiOperation("机构端-提交认证申请")
    public void addAgencyCertification(@RequestBody AgencyCertificationAuditAddReqDTO agencyCertificationAudit) {
        if (ObjectUtil.isNotEmpty(agencyCertificationAudit)){
            AgencyCertificationAudit agencyCertification =  new AgencyCertificationAudit();
            AgencyCertification certification =  new AgencyCertification();
            BeanUtil.copyProperties(agencyCertificationAudit,agencyCertification);
            BeanUtil.copyProperties(agencyCertificationAudit,certification);
            AgencyCertificationAudit byId = agencyAuditService.getById(UserContext.currentUserId());
            if (ObjectUtil.isNotEmpty(byId)){
                agencyCertification.setAuditStatus(0);
                agencyCertification.setCertificationStatus(1);
                agencyCertification.setRejectReason("");
                agencyAuditService.update(agencyCertification,null);
                agencyCertificationService.update(Wrappers.<AgencyCertification>lambdaUpdate().eq(AgencyCertification::getId,byId.getId()).set(AgencyCertification::getCertificationStatus,1));
            }else {
                agencyCertification.setId(UserContext.currentUserId());
                certification.setId(UserContext.currentUserId());
                agencyAuditService.save(agencyCertification);
                certification.setCertificationStatus(1);
                agencyCertificationService.save(certification);
            }
        }else {
            throw new BadRequestException("认证信息不能为空！！");
        }
    }

    /**
     * 机构端-查询最新的驳回原因
     *
     * @return
     */
    @GetMapping("/agency/agency-certification-audit/rejectReason")
    @ApiOperation("机构端-查询最新的驳回原因")
    public HashMap<String, String> selecrAgency() {
        HashMap<String, String> hashMap = new HashMap<>();
        String rejectReason = agencyAuditService.getById(UserContext.currentUserId()).getRejectReason();
        hashMap.put("rejectReason",rejectReason);
        return hashMap;
    }
}
